package com.mdjnu.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// 配置文件
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${spring.mvc.cors.allowed-origins}")
    private String allowedOrigins;
    //  跨域
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")    // 允许跨域访问的路径
                .allowedOrigins(allowedOrigins)
                .allowedHeaders("*")            // 允许请求header的设置
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)         // 允许客户端发送cookie
                .maxAge(3600)                   // 预检请求的缓存时间，单位为秒
                .allowedHeaders("*");           // 允许的请求头
    }

}
